﻿function getBranchStationForLetter() {
    $.getJSON("../order_queryNewOrderSon.json",function (data) {
        $(".branchStation option:gt(0)").remove();
        $(".branchStation").append("<option></option>");
        if(data.success==true){
            $.each(data.users,function (n,value) {
                $(".branchStation").append("<option value='"+value.id+"'>"+value.stationName+"</option>");
            });
        }
    });
}
$(document).ready(function () {
    getBranchStationForLetter();
    var id;
    var branchStation;//支站点
    var middleUser;//中转站确认者
    var middleDate;//中转站确认时间
    var middleMessage;//中转站退回详情
    $(".branchStation").change(function () {
        $(".table-letter-receive tr:gt(0)").remove();
        $.getJSON("../order_midquery.json",{"id":this.value},function (data) {
            $.each(data.orders,function (n,value) {
                $(".table-letter-receive").append("<tr>" +
                    "<td>"+(value.isQuick?"加急件":"")+"</td>" +
                    "<td>"+value.orderNo+"</td>" +
                    "<td>"+value.user.stationAddress+"</td>" +
                    "<td>"+value.city+value.area+value.street+value.address+"</td>" +
                    "<td><button class='btn-letter-received btn btn-primary btn-table'>收到</button><button data-target='#errorMessage' data-toggle='modal' class='btn-send-back btn btn-primary btn-table'>退回</button></td>" +
                    "</tr>");
                $(".btn-letter-received").unbind("click");
                $(".btn-letter-received").click(function () {
                    var orderNo = $(this).parent().parent().children()[1].innerHTML;
                    var $this = $(this);
                    var args = {};
                    args.order = orderNo;
                    args.type = "receive";
                     $.getJSON("../order_deal.json",args,function (msg) {
                         if(msg.success==true){
                             // getCounterForLetter();
                             // getQuickerForLetter();
                             getBranchStationForLetter();
                             $this.parent().parent().remove();
                             window.wxc.xcConfirm(msg.msg, window.wxc.xcConfirm.typeEnum.success);
                         }else {
                             window.wxc.xcConfirm(msg.msg, window.wxc.xcConfirm.typeEnum.success);
                         }
                     });
                });
                $(".btn-send-back").unbind("click");
                $(".btn-send-back").click(function () {
                    var $this = $(this);
                    var orderNo = $(this).parent().parent().children()[1].innerHTML;
                    $(".btn-save-error").unbind("click");
                    $(".btn-save-error").click(function () {
                        var errormsg = $(".input-error-info").val();
                        if(errormsg==""){
                            window.wxc.xcConfirm("请输入反馈信息!", window.wxc.xcConfirm.typeEnum.warning);
                        }else {
                            $.getJSON("../order_deal.json",{"order":orderNo,"middleMessage":errormsg,"type":"sendBack"},function (msg) {
                                if(msg.success==true){
                                    $(".input-error-info").val("");
                                    $this.parent().parent().remove();
                                    $(".close").trigger("click");
                                    // getCounterForLetter();
                                    // getQuickerForLetter();
                                    window.wxc.xcConfirm(msg.msg, window.wxc.xcConfirm.typeEnum.success);
                                }
                                else {
                                    window.wxc.xcConfirm(msg.msg, window.wxc.xcConfirm.typeEnum.error);
                                }
                            });
                        }
                    });
                });
            });

        })
    });

})